<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>写一个深拷贝和浅拷贝的示例</title>
</head>
<body>
  <script>
    // 浅拷贝
    let obj = {
      name: '张三',
      id: 1,
      msg: {
        age: 18
      }
    }
    let obj2 = {}
    for (let k in obj) {
      obj2[k] = obj[k]
    }
    console.log(obj);
    obj2.msg.age = 20
    console.log(obj2);
    // 浅拷贝只是拷贝一层，更深层的对象级别只会拷贝引用
  
    // 深拷贝
    let ject = {
      name: '张三',
      age: 18
    }
  
    // 先转为json格式字符串，再转回来
    let newObj = Object.assign({}, ject)
    
    ject.age = 20;
    console.log(ject, newObj); // 输出18
  </script>
</body>
</html>